Telegram Group & Telegram Channel
🧠 Хитрая задача на Go (v1.22+) — алгоритмы, циклы и подвох в логике

📌 Задача: "Подряд идущие квадраты"

Дано: массив из N положительных целых чисел []int, например:


[]int{1, 4, 9, 16, 25, 36, 50, 64, 81}


Нужно найти максимальную длину подмассива, где все элементы — квадраты подряд идущих натуральных чисел.

Например, [4, 9, 16, 25] — это квадраты 2², 3², 4², 5² → длина = 4
[50, 64] — не подходят, так как 50 не является точным квадратом.

🎯 Формат функции:


func MaxConsecutiveSquares(nums []int) int


Пример:


input := []int{1, 4, 9, 16, 25, 36, 50, 64, 81}
fmt.Println(MaxConsecutiveSquares(input)) // 👉 6


🧩 Подвох:

- Часто пытаются сравнивать разности или применять хэшмапы — но это ошибка
- Нужно восстановить корни чисел (`sqrt`) и убедиться, что они натуральные и идут подряд
- Важно использовать math.Sqrt и быть осторожным с плавающей точкой (`float64`)
- Также важно не выходить за границы slice

💡 Подсказка:

```go
import "math"

func isSquare(n int) (int, bool) {
root := int(math.Sqrt(float64(n)))
return root, root*root == n
}
```

🛠 **Что проверяет задача:**

• Умение работать с нецелыми корнями
• Алгоритмы "двойного указателя" (две границы окна)
• Понимание строгой проверки на натуральные числа
• Внимание к float64-погрешности и типам



tg-me.com/golangtests/777
Create:
Last Update:

🧠 Хитрая задача на Go (v1.22+) — алгоритмы, циклы и подвох в логике

📌 Задача: "Подряд идущие квадраты"

Дано: массив из N положительных целых чисел []int, например:


[]int{1, 4, 9, 16, 25, 36, 50, 64, 81}


Нужно найти максимальную длину подмассива, где все элементы — квадраты подряд идущих натуральных чисел.

Например, [4, 9, 16, 25] — это квадраты 2², 3², 4², 5² → длина = 4
[50, 64] — не подходят, так как 50 не является точным квадратом.

🎯 Формат функции:


func MaxConsecutiveSquares(nums []int) int


Пример:


input := []int{1, 4, 9, 16, 25, 36, 50, 64, 81}
fmt.Println(MaxConsecutiveSquares(input)) // 👉 6


🧩 Подвох:

- Часто пытаются сравнивать разности или применять хэшмапы — но это ошибка
- Нужно восстановить корни чисел (`sqrt`) и убедиться, что они натуральные и идут подряд
- Важно использовать math.Sqrt и быть осторожным с плавающей точкой (`float64`)
- Также важно не выходить за границы slice

💡 Подсказка:

```go
import "math"

func isSquare(n int) (int, bool) {
root := int(math.Sqrt(float64(n)))
return root, root*root == n
}
```

🛠 **Что проверяет задача:**

• Умение работать с нецелыми корнями
• Алгоритмы "двойного указателя" (две границы окна)
• Понимание строгой проверки на натуральные числа
• Внимание к float64-погрешности и типам

BY Go tests


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golangtests/777

View MORE
Open in Telegram


Go tests Telegram | DID YOU KNOW?

Date: |

Why Telegram?

Telegram has no known backdoors and, even though it is come in for criticism for using proprietary encryption methods instead of open-source ones, those have yet to be compromised. While no messaging app can guarantee a 100% impermeable defense against determined attackers, Telegram is vulnerabilities are few and either theoretical or based on spoof files fooling users into actively enabling an attack.

What Is Bitcoin?

Bitcoin is a decentralized digital currency that you can buy, sell and exchange directly, without an intermediary like a bank. Bitcoin’s creator, Satoshi Nakamoto, originally described the need for “an electronic payment system based on cryptographic proof instead of trust.” Each and every Bitcoin transaction that’s ever been made exists on a public ledger accessible to everyone, making transactions hard to reverse and difficult to fake. That’s by design: Core to their decentralized nature, Bitcoins aren’t backed by the government or any issuing institution, and there’s nothing to guarantee their value besides the proof baked in the heart of the system. “The reason why it’s worth money is simply because we, as people, decided it has value—same as gold,” says Anton Mozgovoy, co-founder & CEO of digital financial service company Holyheld.

Go tests from sa


Telegram Go tests
FROM USA